import { _decorator, Component, instantiate, Label, log, Node, Prefab, Sprite, SpriteFrame, tween } from 'cc';
import { PrefsManager } from './PrefsManager';
import { NumberIncrementLabel } from './NumberIncrementLabel';
import { ResourceUtil } from '../AshFramework/Utils/ResourceUtil';
import { GoldAwad } from './GoldAwad';
import { GameManager } from './GameManager';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
const { ccclass, property } = _decorator;

@ccclass('SettlePanel')
export class SettlePanel extends Component {
    public static Instance: SettlePanel = null;

    @property(Node)
    GJ: Node = null;

    @property(Sprite)
    DownSprite: Sprite = null;

    @property(Sprite)
    UpSprite: Sprite = null;

    @property(SpriteFrame)
    DownSFs: SpriteFrame[] = [];

    @property(SpriteFrame)
    UpSFs: SpriteFrame[] = [];

    @property(Label)
    Reward: Label = null;

    @property(Node)
    ProgressNode: Node = null;

    @property(Node)
    AwardStartNode: Node = null;

    @property(Node)
    AwardTargetNode: Node = null;

    Multiple: number = 1;

    FillRange: number = 0;

    private _rewardNum: number = 0;
    private _pendantIndex: number = 0;

    protected onLoad(): void {
        SettlePanel.Instance = this;
    }

    protected start(): void {
        this.FillRange = PrefsManager.Instance.userData.FillRange;
        this._pendantIndex = PrefsManager.Instance.userData.unluckGS[PrefsManager.Instance.userData.unluckGS.length - 1];
        this.showUI();
    }

    showUI() {
        this._rewardNum = GameManager.Instance.Award;
        this.Reward.string = this._rewardNum.toString();
        this.showSprite();
    }

    showSprite() {
        if (this._pendantIndex <= this.DownSFs.length - 1) {
            this.GJ.active = true;
            this.DownSprite.spriteFrame = this.DownSFs[this._pendantIndex];
            this.UpSprite.spriteFrame = this.UpSFs[this._pendantIndex];
            this.UpSprite.fillRange = this.FillRange / 100;
            this.FillRange += 20;
            if (this.FillRange >= 100) {
                this.FillRange = 100;
            }
            this.playFillRange();
        } else {
            this.GJ.active = false;
        }
    }

    playFillRange() {
        AudioManager.PlaySound(Audios.RiseProgress);
        this.ProgressNode.getComponent(NumberIncrementLabel).playNumberIncrementTo(this.FillRange, 1);
        tween(this.UpSprite)
            .to(1, { fillRange: this.FillRange / 100 }, { easing: `quintIn` })
            .call(() => {
                if (this.FillRange >= 100) {
                    PrefsManager.Instance.userData.FillRange = 0;
                    PrefsManager.Instance.userData.unluckGS.push(this._pendantIndex + 1);
                } else {
                    PrefsManager.Instance.userData.FillRange = this.FillRange;
                }
                PrefsManager.Instance.saveData();
            })
            .start();
    }

    getAward() {
        ResourceUtil.LoadPrefab("GoldAward").then((prefab: Prefab) => {
            const award: Node = instantiate(prefab);
            award.parent = this.node;
            award.children.forEach(e => {
                e.getComponent(GoldAwad).init(this.AwardStartNode.getWorldPosition().clone(), this.AwardTargetNode.getWorldPosition().clone());
            })
            PrefsManager.Instance.userData.Gold += this._rewardNum * this.Multiple;
            PrefsManager.Instance.saveData();
            GameManager.Instance.Gold.playNumberIncrementTo(PrefsManager.Instance.userData.Gold);
        })
    }

}


